package com.gitee.chili.leisure.message;

import com.gitee.chili.leisure.RoomManager;
import com.gitee.chili.leisure.core.AbstractDefaultMessage;
import com.gitee.chili.leisure.core.Packet;
import com.gitee.chili.leisure.exception.BusinessException;
import com.gitee.chili.leisure.game.MahjongPlayer;
import com.gitee.chili.leisure.game.RoomDetail;
import com.gitee.chili.leisure.model.MahjongModel;
import com.gitee.chili.leisure.util.AuxiliaryRoom;
import com.gitee.chili.leisure.util.TaskManager;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 玩家操作返回
 *
 * @author week solider
 * @since 2020.09.23
 */
@Slf4j
public class MemberOperate extends AbstractDefaultMessage {


    private MahjongModel.PlayerOperateResult operateResult;

    private MahjongPlayer player;

    public MemberOperate(Packet packet, Channel channel) {
        super(packet, channel);
    }

    @Override
    protected void decoder(byte[] body) throws InvalidProtocolBufferException {

        operateResult = MahjongModel.PlayerOperateResult.parseFrom(body);

        if (Objects.isNull(operateResult)) {
            throw new BusinessException("操作异常");
        }
    }

    @Override
    public Packet parseBody() {

        MahjongModel.PlayerOperateResult.OperateInfoModel model = operateResult.getOperateInfoModel();

        RoomDetail roomDetail = RoomManager.obtain(getRoomNum());

        MahjongPlayer player = (MahjongPlayer) roomDetail.obtain(getMemberId());

        switch (model.getType()) {
            case UNRECOGNIZED:
                log.error("未识别的操作类型: {}", model.getType(), new BusinessException("未识别的操作类型"));
                return null;
            case AUTO_DISCARD:
                // 自动出牌
            case MANY_WINNER:
                // 玩家不可操作类型 (一炮多响)
            case DRAW_CARD:
                // 玩家不可主动摸牌
            case READY_HAND:
                // TODO 暂时没有听的业务情况所以不处理 (适用于二人麻将)
                // 主动听牌 (卡五星中还有亮牌等操作)
                log.error("操作类型错误 : {}", model.getType(), new BusinessException("操作类型发生异常，违规操作"));
                return null;

            // --------- 以下是需要处理的类型

            case CONCEALED_KONG:
                // 暗杠 (这时候一定是自己的轮子，就是一定是轮到自己出牌了，才可以暗杠)
                break;
            case EXPOSED_KONG:
                // 补杠
                break;
            case DISCARD:
                String name = String.format(TaskManager.TaskNames.discardName, player.getUserId());
                // 清除定时器
                TaskManager.clear(name);
                // 出牌
                return AuxiliaryRoom.discard(player, operateResult);
            case WINNER:
                // 胡
                return AuxiliaryRoom.winner(player, operateResult);
            case PASS:
                // 过
                return AuxiliaryRoom.pass(operateResult);
            case KONG:
                // 点杠
                break;
            case CHOW:
                // TODO 吃 (暂时没有吃操作)
                break;
            case PONG:
                // 碰
                break;
        }

        return null;
    }

    /**
     * 获取玩家操作类型
     */
    public MahjongModel.OptionsType getOperateType() {
        return operateResult.getOperateInfoModel().getType();
    }

    public MahjongModel.PlayerOperateResult getOperateResult() {
        return operateResult;
    }

    /**
     * 获取玩家信息
     */
    public MahjongPlayer getPlayer() {
        return player;
    }

}
